home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3785 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.4 KB

  1. Path: brighton.openmarket.com!decwrl!purdue!yuma!steffend
  2. From: steffend@lamar.colostate.edu (Dave Steffen)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: First C++ Program.......
  5. Date: 26 Jan 1996 01:32:15 GMT
  6. Organization: Colorado State University, Fort Collins, CO  80523
  7. Message-ID: <4e9auv$325i@yuma.ACNS.ColoState.EDU>
  8. References: <4e5qeu$7f0@server1.ctc.com>
  9. NNTP-Posting-Host: glitch.physics.colostate.edu
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Michael D. Aesoph (aesoph@ctc.com) wrote:
  13. > Dear Collective:
  14.  
  15. >      I an a C programmer that has recently been tasked to do some 
  16. > programming in C++.  Virtually all of my work involves data acquisition 
  17. > of some kind and I normally use National Instrument's LabWindows/CVI as 
  18. > my programming environment.  In standard programming, things are 
  19. > relatively simple...  Configure data acquisition, acquire data, plot 
  20. > data, analyze data, save data, etc. etc.  Now, in C++, there's all this 
  21. > talk about classes, objects, and other things.  The project manager wants 
  22. > a "sensor" class....  OK, where's the link???  I still have to do all of 
  23. > the tasks mentioned before, but embedded apparently in a class????  
  24. > Please advise..  I know that this is the classic stumbling block for 
  25. > beginning C++ programmers, so don't make fun of me!!!!
  26.  
  27. >                                      Michael D. Aesoph
  28.  
  29.     OK, I'll take a swing at it...
  30.  
  31.     First off, is there a particular reason your boss is making
  32. you do this? I mean, maybe there's a real need to go OO for this -
  33. like, a complicated interface to your sensors and screwing up the
  34. parameters in a function call means the control rods get pulled out
  35. of the reactor - BOOM! Or is this just your boss jumping on the "OO
  36. Bandwagon" and making everyone write in C++ because it's trendy? Of
  37. course, either way you've got to do it (assuming you like your
  38. job). But in the first case, you need to do some real OO programming,
  39. while in the latter case, you just need to make it _look_ like you're
  40. doing OO, while really keeping things the way they are (because the
  41. way things are works just fine).
  42.  
  43.     Don't laugh - I've got a friend who's a (reasonably) highly
  44. paid C++ expert, he's been on more projects where C++ is a mistake
  45. than otherwise!
  46.  
  47.     OK, so assuming you're really going to do this and do it
  48. right... Everything goes into the class. Pointers to memory locations,
  49. sensor state variables, functions to read and change state variables,
  50. functions to read from the sensor, functions to initialize the
  51. sensor... everything.
  52.  
  53.     Your programs then look something like this:
  54.  
  55.  
  56. HallProbe Selector (port_7);
  57.  
  58. /*
  59. HallProbe is a class you've defined somewhere to run
  60. magnetic sensors. This one is called "Selector" (measures the mag
  61. field in a selector or something) and is the one plugged into port
  62. 7.
  63. /*
  64.  
  65. // or, alternatively
  66.  
  67. Sensor Selector (HallProbe, port_7)
  68.  
  69. /*
  70. Sensor is a generic sensor class, HallProbe is a value of some
  71. enumerated type - this tells Selector that it's a mag field probe, as
  72. opposed to a pressure sensor.
  73. */
  74.  
  75.  
  76. // Now initialize it, if the constructor didn't already
  77.  
  78. Selector.Initialize (high_gain);
  79.  
  80. // high_gain is some value that tells the sensor what sensitivity to
  81. // use
  82.  
  83. if (Selector.Errorstate() == OK)
  84.  
  85. /*
  86. Check state of Selector. OK is some constant defined to match some
  87. return value of the Selector::Errorstate() member function...
  88. */
  89.  
  90.  
  91.     The nice thing about this is that you can treat your sensors
  92. in your code like they are in the real world - an independent chunk of
  93. equipment. If the interface to your sensors is really complicated and
  94. hard to use, "wrapping" this complicated interface in C++ classes can
  95. be a Godsend (I use the fortran LAPACK library all the time, and the
  96. function calls are unbelievably ugly... but my C++ code is beautiful).
  97.     On the other hand, if the interface is relatively simple,
  98. going OO won't get you much unless you're doing something really
  99. complicated with your sensors.
  100.  
  101.     OK, does this make any sense? Let me know if it doesn't and
  102. I'll try again. ;-)
  103.  
  104.                                  /\
  105.                                  \/
  106.  
  107. Dave Steffen                      No, his mind is not for rent
  108. Dept. of Physics                  To any God or Government
  109. Colorado State University         Always hopeful, yet discontent
  110. steffend@lamar.colostate edu      He knows changes aren't permanent-
  111.                       But change is...
  112. "Speak softly...                    
  113. ... and carry a black belt!"              -Neal Peart / RUSH
  114. -----------------------------------------------------------------------
  115.  
  116.  
  117.  
  118.